package com.wdress.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 128bits code generator.
 * @authro zhouqiang
 * @version 1.1
 */
public class Encrypt {
	private static Log logger = LogFactory.getLog(Encrypt.class);
	
	/**
	 * Arithemic:MD5
	 */
	public static final String MD5="MD5";
	/**
	 * Arithemic: SHA
	 */
	public static final String SHA = "SHA";

	public static String change(String ArithemicName, String Message) {
		return change(ArithemicName, Message, true);
	}

	/**
	 * @param ArithemicName
	 * @param Message
	 * @return
	 */
	public static String change(String ArithemicName, String Message,
			boolean change) {
		if (!change) {
			return Message;
		}
		String Digest = "";
		MessageDigest currentAlgorithm;
		try {
			currentAlgorithm = MessageDigest.getInstance(ArithemicName);
			currentAlgorithm.reset();
			byte[] mess = Message.getBytes();
			byte[] hash = currentAlgorithm.digest(mess);
			for (int i = 0; i < hash.length; i++) {
				int v = hash[i];
				if (v < 0) {
					v = 256 + v;
				}
				if (v < 16) {
					Digest += "0";
				}
				Digest += Integer.toString(v, 16).toUpperCase() + "";
			}
		} catch (NoSuchAlgorithmException e) {
			logger.debug("you select algorithm not have");
		}
		return Digest;

	}
}
